package com.wei.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wei.entity.Subject;
import com.wei.entity.excel.SubjectData;
import com.wei.service.SubjectService;
import com.wei.service_base.exception.XueYuanException;
import org.springframework.stereotype.Service;

/**
 * @author wjx
 * @ClassName SubjectExcelListener
 * @description: TODO
 * @date 2022-11-29 17:17
 * @version: 1.0
 */
@Service
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {
//因为ExcelListener不能交给Spring进行管理，需要自己new，不能注入其他对象

    private SubjectService subjectService;

    public SubjectExcelListener(SubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 一行一行读取文件
     *
     * @param subjectData
     * @param analysisContext
     */
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        //excel为空
        if (subjectData == null) {
            throw new XueYuanException(1000, "文件数据为空");
        }

        //一级分类名称
        String oneSubjectName = subjectData.getOneSubjectName();
        //二级分类名称
        String twoSubjectName = subjectData.getTwoSubjectName();

        //做一个事情，相同名称只能添加一次
        Subject existOneSubject = existOneSubject(subjectService, oneSubjectName);

        if (existOneSubject == null) {

            existOneSubject = new Subject();

            existOneSubject.setParentId("0");
            existOneSubject.setTitle(subjectData.getOneSubjectName());

            subjectService.save(existOneSubject);
        }

        //得到一级分类的id。
        //注意：当一级分类没信息时，这里会有空指针异常
        String pid = existOneSubject.getId();

        if (existTwoSubject(subjectService, twoSubjectName, pid) == null) {

            Subject twoSubject = new Subject();
            twoSubject.setTitle(twoSubjectName);
            twoSubject.setParentId(pid);

            subjectService.save(twoSubject);
        }
    }

    /**
     * 一级分类不能重复添加
     *
     * @param subjectService
     * @param name
     * @return
     */
    private Subject existOneSubject(SubjectService subjectService, String name) {

        Subject subject = new Subject();

        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Subject::getTitle, name);
        queryWrapper.eq(Subject::getParentId, "0");

        Subject search = subjectService.getOne(queryWrapper);

        return search;
    }

    /**
     * 二级分类不能重复添加
     *
     * @param subjectService
     * @param name
     * @param id
     * @return
     */
    private Subject existTwoSubject(SubjectService subjectService, String name, String id) {
        Subject subject = new Subject();

        LambdaQueryWrapper<Subject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Subject::getTitle, name);
        queryWrapper.eq(Subject::getParentId, id);

        Subject search = subjectService.getOne(queryWrapper);

        return search;
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

}
